<?php
/**
 * adminPanel - Admin Section for WordPress Vampire plugins
 * 
 * @author Long Nguyen Hai
 * @copyright 2011
 * @since 1.0
 */

class WV_AdminPanel{
	
	// constructor
	function WV_AdminPanel() {
		global $wv;

        // Add the script and style files
        add_action('admin_print_scripts', array(&$this, 'load_scripts'));
        add_action('admin_print_styles', array(&$this, 'load_styles') );

        //Add tinyMCE plugin
        $this->add_mce();

		// Add the admin menu
		add_action('admin_menu', array(&$this, 'add_menu'));
	}

    function add_mce() {
        if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) return;

        if (get_user_option('rich_editing') == 'true') {
            add_filter('mce_external_plugins', array(&$this, 'add_mce_plugin'));
            add_filter('mce_buttons', array(&$this, 'add_mce_button'));
            add_action('admin_footer', array(&$this, 'dialog'));
        }
    }

    function add_mce_plugin($plugins) {
        $plugins['wv_spinner'] = WV_URLPATH . 'admin/js/wv-mce.js';
        return $plugins;
    }

    function add_mce_button($buttons) {
        array_push($buttons, 'separator', 'wv_add_spinner', 'wv_preview_spinner');
        return $buttons;
    }

    function dialog() {
        require_once (dirname(__FILE__) . '/views/dialog.php');
    }
	
	function add_menu()  {
        add_submenu_page('options-general.php', __("WordSpinner Settings", 'wv'), __("Word Spinner", 'wv'), 8, 'wv_spinner', array(&$this, 'show_menu'));
	}
	
	// load the script for the defined page and load only this code	
	function show_menu() {
        $disallowed = array('|', '||', '+');

        // variables for the field and option names
        $field1 = 'wspin_split';
        $field2 = 'wspin_left';
        $field3 = 'wspin_right';
        $hidden_field_name = 'wspin_hidden';

        // See if the user has posted us some information
        // If they did, this hidden field will be set to 'Y'
        if ($_POST[$hidden_field_name] == 'Y') {

            if ((in_array($_POST[$field1], $disallowed)) || (in_array($_POST[$field2], $disallowed)) || (in_array(
                $_POST[$field3], $disallowed))) {
                // Put an options updated message on the screen
                echo ('<div class="updated"><p><strong>OPTIONS WERE NOT UPDATED!</strong></p>
				  <p>Part of that combination has been found to produce unexpected results. Please dont use any of the following:<br /><br />');

                foreach ($disallowed as $value) {
                    echo $value . "&nbsp;&nbsp;";
                }

                echo ('</p></div>');

            } else {
                // Save the posted value in the database
                update_option('wspin_splitchar', $_POST[$field1]);
                update_option('wspin_leftchar', $_POST[$field2]);
                update_option('wspin_rightchar', $_POST[$field3]);

                // Put an options updated message on the screen
                echo ('<div class="updated"><p><strong>Options Saved</strong></p></div>');
            }
        }

        // Now display the options editing screen
        require_once (dirname(__FILE__) . '/views/settings.php');
	}
	
	function load_scripts() {
		global $current_screen;
			
		wp_enqueue_script('jquery');

		switch ($current_screen->id) {
			case 'page':
			case 'post':
				wp_enqueue_script('jquery-ui', WV_URLPATH . 'admin/js/jquery-ui-1.7.3.custom.min.js', array('jquery'));
				wp_enqueue_script('wvspinner', WV_URLPATH . 'admin/js/wv-mce.js', array('jquery'));

		        wp_localize_script('wvspinner',
		                           'wvSettings',
		                           array('url' => WV_URLPATH,
		                               'nonce' => wp_create_nonce('wv_word_spinner'),
		                                'leftchar' => esc_js(get_option('wspin_leftchar')),
		                                'rightchar' => esc_js(get_option('wspin_rightchar')),
		                                'splitchar' => esc_js(get_option('wspin_splitchar'))));
				break;
			default:
				# code...
				break;
		}
		
	}
	
	function load_styles() {
		global $current_screen;
		
		wp_enqueue_style( 'wvglobal', WV_URLPATH .'admin/css/global.css' );
		
		switch ($current_screen->id) {
			case 'page':
			case 'post':
				wp_enqueue_style( 'jquery-ui', WV_URLPATH .'admin/css/smoothness/jquery-ui-1.7.3.custom.css' );
				break;
			default:
				# code...
				break;
		}
	}
	
}